
package com.sausageking.auth;

import java.io.IOException;

import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.sausageking.shared.Consumer;

@SuppressWarnings("serial")
public class SignupServlet extends HttpServlet {

  public void doGet(HttpServletRequest req, HttpServletResponse resp)
      throws IOException {
    String email = req.getParameter("email");
    String password = req.getParameter("password");
    String passwordHash = UserLoginUtil.generatePasswordHash(password);

    // Register the new consumer - save it into the datastore.
    Consumer consumer = new Consumer(email, passwordHash);
    ConsumerDao customerDao = new ConsumerDao();
    customerDao.saveConsumer(consumer);

    // login the user in the current session.
    loginUser(consumer, req);

    resp.setContentType("text/plain");
    resp.getWriter().println("Persistend User: " + consumer.getEmail());
  }

  private void loginUser(Consumer consumer, HttpServletRequest req) {
    req.getSession().setAttribute("consumer", consumer);
  }
}
